package com.yifang.yibingproject.config;

import com.yifang.yibingproject.service.SysLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.client.RestTemplate;

import java.io.File;

@Order(1)
@Configuration
public class StartConfig implements ApplicationRunner {
    private static final Logger log = LoggerFactory.getLogger(com.yifang.yibingproject.config.StartConfig.class);

    @Value("${spring.servlet.multipart.location}")
    private String temp;

    @Autowired
    private SysLogService sysLogService;

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("---------【启动完成】检查缓存目录-----------------------");
        checkTempDirectory();

        log.info("---------【启动完成】检查或创建日志表-------------------");
        sysLogService.checkTableIfNotCreate();
    }

    /**
     * 检查文件上传缓存目录是否存在，若不存在创建该目录
     */
    private void checkTempDirectory() {
        File file = new File(temp);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
        }
    }
}
